<script setup >
import { ref, reactive } from "vue";
import { NScrollbar, NPopover, NButton, NSpin, useMessage } from "naive-ui";
import store from "@/store";
import router from "@/router";

const message = useMessage();

store.dispatch("findCart").then(() => {});
let show = ref(false);
const delCart = (skuId) => {
  show.value = true;
  store.dispatch("deleteCart", skuId).then(() => {
    show.value = false;
  });
};

// 结算下单
const checkOut = () => {
  // 弹出确认框 提示 下单结算需要登录
  router.push("/member/checkout");
  // dialog.warning({
  //   title: "警告",
  //   content: `下单结算需要登录，确认现在去登录吗？`,
  //   positiveText: "确定",
  //   negativeText: "取消",
  //   onPositiveClick: () => {
  //     router.push("/login");
  //   },
  // });
  // 使用导航守卫  需要登录的拦截到登录页面
};
</script>

<template>
  <div class="cart">
    <n-popover trigger="hover">
      <template #trigger>
        <router-link class="curr" to="/cart">
          <i class="iconfont icon-gouwuchekong icons"></i>
          <em>{{ store.getters.validTotal }}</em>
        </router-link>
      </template>
      <div class="pop-cart">
        <n-spin :show="show">
          <div class="info">
            <n-scrollbar>
              <div class="list" v-if="store.getters.validTotal > 0">
                <div
                  class="item"
                  v-for="goods in store.getters.validList"
                  :key="goods.skuId"
                >
                  <RouterLink :to="`/product/${goods.id}`">
                    <img :src="goods.picture" alt="" />
                    <div class="center">
                      <p class="name ellipse2">
                        {{ goods.name }}
                      </p>
                      <p class="attr ellipse">{{ goods.attrsText }}</p>
                    </div>
                    <div class="right">
                      <p class="price">&yen;{{ goods.nowPrice }}</p>
                      <p class="count">x{{ goods.count }}</p>
                    </div>
                  </RouterLink>
                  <i
                    class="iconfont icon-close"
                    @click="delCart(goods.skuId)"
                  ></i>
                </div>
              </div>
              <div v-else>
                <div class="info-img">
                  <img
                    src="@/assets/images/d0736561-5222-4653-a10c-a933170e9e80.png"
                    alt=""
                  />
                  <p>购物车没有商品呢</p>
                </div>
              </div>
            </n-scrollbar>
          </div>
        </n-spin>

        <div class="tool">
          <div>
            <span>共 {{ store.getters.validTotal }} 件商品</span
            ><span>￥{{ store.getters.validAmount }}</span>
          </div>
          <div>
            <n-button type="primary" @click="checkOut"> 去结算 </n-button>
          </div>
        </div>
      </div>
    </n-popover>
  </div>
</template>

<style scoped lang='less'>
.cart {
  width: 50px;
  .curr {
    height: 32px;
    line-height: 32px;
    text-align: center;
    position: relative;
    display: block;
    .icons {
      font-size: 22px;
      color: #555;
    }
    em {
      font-style: normal;
      position: absolute;
      right: 0;
      top: 0;
      padding: 1px 6px;
      line-height: 1;
      background: @helpColor;
      color: #fff;
      font-size: 12px;
      border-radius: 10px;
      font-family: Arial;
    }
  }
}

.pop-cart {
  height: 300px;
  width: 350px;
  position: relative;
  .info {
    height: 250px;
    width: 100%;
    .info-img {
      user-select: none;
      display: flex;
      flex-flow: column nowrap;
      justify-content: center;
      width: 100%;
      padding: 30px;
      img {
        height: 160px;
        width: 100%;
        object-fit: contain;
      }
      p {
        font-size: 15px;
        font-weight: 500;
        text-align: center;
        color: #999;
      }
    }
    .item {
      border-bottom: 1px solid #f5f5f5;
      padding: 10px 0;
      position: relative;
      i {
        position: absolute;
        bottom: 38px;
        right: 25px;
        opacity: 0;
        color: #666;
        transition: all 0.5s;
      }
      &:hover {
        i {
          opacity: 1;
          cursor: pointer;
        }
      }
      a {
        display: flex;
        align-items: center;
        img {
          height: 80px;
          width: 80px;
        }
        .center {
          padding: 0 10px;
          width: 150px;
          .name {
            font-size: 14px;
          }
          .attr {
            color: #999;
            padding-top: 5px;
            font-size: 13px;
          }
        }
        .right {
          width: 70px;
          padding-right: 0px;
          text-align: center;
          .price {
            font-size: 15px;
            color: @priceColor;
          }
          .count {
            color: #999;
            margin-top: 5px;
            font-size: 14px;
          }
        }
      }
    }
  }
  .tool {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50px;
    width: 100%;
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid #f5f5f5;
    padding-top: 5px;
    & > :first-child {
      display: flex;
      flex-flow: column nowrap;
      color: #666;
      & > :first-child {
        font-size: 13px;
      }
      & > :last-child {
        color: @priceColor;
        font-size: 18px;
      }
    }
  }
}
</style>